home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************\
-
- File: prefs.c
-
- Purpose: This module handles the preferences file, which contains
- the real filename of the current startup screen.
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program in a file named "GNU General Public License".
- If not, write to the Free Software Foundation, 675 Mass Ave,
- Cambridge, MA 02139, USA.
-
- \**********************************************************************/
-
- #include "prefs.h"
- #include "environment.h"
- #include "program globals.h"
-
- static PrefStruct thePrefs;
- static long gPrefsFilePos;
-
- short PreferencesInit(void)
- {
- short prefsFileID;
- short err;
-
- err=OpenPrefsFile(&prefsFileID);
- if (err!=prefs_allsWell)
- {
- if ((err==prefs_diskReadErr) || (err==prefs_diskWriteErr) || (err==prefs_virginErr))
- ClosePrefsFile(prefsFileID);
- return err;
- }
-
- GetNextPrefs(prefsFileID);
- if (err!=prefs_allsWell)
- {
- ClosePrefsFile(prefsFileID);
- return err;
- }
-
- CopyPrefsToGlobals();
- ClosePrefsFile(prefsFileID);
-
- return prefs_allsWell;
- }
-
- short OpenPrefsFile(short *prefsFileID)
- {
- short thisFile;
- OSErr isHuman;
- short vRefNum;
- long dirID;
- FSSpec prefsFile;
- FInfo prefsInfo;
- short temp;
- long count;
- Boolean newPrefs;
- short err;
- unsigned char *name=PREFS_FILE_NAME;
-
- newPrefs=FALSE;
- isHuman=FindFolder(kOnSystemDisk, 'pref', kCreateFolder, &vRefNum, &dirID);
-
- if (isHuman!=noErr)
- return prefs_cantOpenPrefsErr;
-
- if (gHasFSSpecs)
- {
- isHuman=FSMakeFSSpec(vRefNum, dirID, name, &prefsFile);
- if (isHuman!=noErr)
- {
- if (isHuman==fnfErr)
- {
- isHuman=FSpCreate(&prefsFile, CREATOR, PREFS_TYPE, 0);
- if (isHuman!=noErr)
- return prefs_cantCreatePrefsErr;
- newPrefs=TRUE;
- }
- else return prefs_cantOpenPrefsErr;
- }
- isHuman=FSpOpenDF(&prefsFile, fsRdWrPerm, &thisFile);
- *prefsFileID=thisFile;
- if (isHuman!=noErr)
- return prefs_cantOpenPrefsErr;
- }
- else
- {
- isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
- *prefsFileID=thisFile;
- if (isHuman!=noErr)
- {
- if (isHuman==fnfErr)
- {
- isHuman=HCreate(vRefNum, dirID, name, CREATOR, PREFS_TYPE);
- if (isHuman!=noErr)
- return prefs_cantCreatePrefsErr;
- prefsInfo.fdType=PREFS_TYPE;
- prefsInfo.fdCreator=CREATOR;
- prefsInfo.fdFlags=0;
- prefsInfo.fdLocation.h=prefsInfo.fdLocation.v=0;
- prefsInfo.fdFldr=0;
- isHuman=HSetFInfo(vRefNum, dirID, name, &prefsInfo);
- if (isHuman!=noErr)
- return prefs_cantCreatePrefsErr;
- isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
- *prefsFileID=thisFile;
- if (isHuman!=noErr)
- return prefs_cantOpenPrefsErr;
- newPrefs=TRUE;
- }
- else return prefs_cantOpenPrefsErr;
- }
- }
- if (newPrefs)
- {
- return Virgin(*prefsFileID);
- }
-
- return prefs_allsWell;
- }
-
- void ClosePrefsFile(short prefsFileID)
- {
- FSClose(prefsFileID);
- FlushVol(0L, kOnSystemDisk);
- }
-
- short GetNextPrefs(short prefsFileID)
- {
- OSErr isHuman;
- long count;
-
- count=sizeof(thePrefs);
- isHuman=FSRead(prefsFileID, &count, &thePrefs);
- if (isHuman!=noErr)
- return prefs_diskReadErr;
-
- return prefs_allsWell;
- }
-
- short SavePrefs(short prefsFileID)
- {
- long oldEOF;
- OSErr isHuman;
- long count;
-
- isHuman=SetEOF(prefsFileID, sizeof(thePrefs));
- if (isHuman!=noErr)
- return prefs_diskWriteErr;
-
- SetFPos(prefsFileID, 1, 0L);
- count=sizeof(thePrefs);
- isHuman=FSWrite(prefsFileID, &count, &thePrefs);
- if (isHuman!=noErr)
- return prefs_diskWriteErr;
-
- return prefs_allsWell;
- }
-
- short Virgin(short prefsFileID)
- {
- short err;
-
- DefaultPrefs();
- CopyGlobalsToPrefs();
- err=SavePrefs(prefsFileID);
-
- return (err==prefs_allsWell) ? prefs_virginErr : err;
- }
-
- void DefaultPrefs(void)
- {
- unsigned char *temp="\pOld StartupScreen";
- short i;
-
- for (i=temp[0]; i>=0; i--)
- gLastName[i]=temp[i];
- }
-
- void CopyGlobalsToPrefs(void)
- {
- short i;
-
- for (i=0; i<32; i++)
- thePrefs.name[i]=0x00;
- for (i=gLastName[0]; i>=0; i--)
- thePrefs.name[i]=gLastName[i];
- }
-
- void CopyPrefsToGlobals(void)
- {
- short i;
-
- for (i=thePrefs.name[0]; i>=0; i--)
- gLastName[i]=thePrefs.name[i];
- }
-
- void SaveThePrefs(void)
- {
- short prefsFileID;
-
- OpenPrefsFile(&prefsFileID);
- CopyGlobalsToPrefs();
- SavePrefs(prefsFileID);
- ClosePrefsFile(prefsFileID);
- }
-